# -*- coding: utf-8 -*-
import unittest
import re
retype = type(re.compile(''))
class AbstractAddAssertTestCase(unittest.TestCase):
    def assertEqualsList(self,expected,actual):
        self.assertEquals(len(expected),len(actual))
        for i in range(len(expected)):
            self.assertEquals(expected[i],actual[i])
            
            
    def assertAlmostEquals2(self,expect,actual,bound=10,message=''):
        '''近似值比较'''
        if abs(expect - actual) > bound:
            raise self.failureException, \
                  (message or '%r != %r within %r bound' % (expect, actual, bound))
                  
            
    #copy from test_optparse.py
    def assertRaises2(self,
                     func,
                     args,
                     kwargs,
                     expected_exception,
                     expected_message):

        """
        Assert that the expected exception is raised when calling a
        function, and that the right error message is included with
        that exception.

        Arguments:
          func -- the function to call
          args -- positional arguments to `func`
          kwargs -- keyword arguments to `func`
          expected_exception -- exception that should be raised
          expected_message -- expected exception message (or pattern
            if a compiled regex object)

        Returns the exception raised for further testing.
        """
        if args is None:
            args = ()
        if kwargs is None:
            kwargs = {}
        if type(args) is not tuple:
            raise Exception,"args must tuple,but:"+str(args) 

        try:
            func(*args, **kwargs)
        except expected_exception, err:
            actual_message = str(err)
            if isinstance(expected_message, retype):
                self.assert_(expected_message.search(actual_message),
                             """\
expected exception message pattern:
/%s/
actual exception message:
'''%s'''
""" % (expected_message.pattern, actual_message))
            else:
                self.assertEqual(actual_message,
                                 expected_message,
                                 """\
expected exception message:
'''%s'''
actual exception message:
'''%s'''
""" % (expected_message, actual_message))

            return err
        else:
            self.fail("""expected exception %(expected_exception)s not raised
called %(func)r
with args %(args)r
and kwargs %(kwargs)r
""" % locals ())

